﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace net.readify.knockknock
{
    
    public class RedPill : IRedPill
    {      
        public Guid WhatIsYourToken()
        {
            return Guid.Parse("781e8caa-5ea8-470b-a3ee-457960022778");
        }
        
        public string ReverseWords(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                string ReversedWord = "";
                List<string> WordBlocksList = SplitStringOverSpace(s);

                //Reverse currrent word
                foreach (string w in WordBlocksList)
                {
                    if (w.Length > 0)
                    {
                        for (int i = w.Length - 1; i > -1; i--)
                            ReversedWord += w[i].ToString();
                    }
                }
                return ReversedWord;
            }
            else
                return "";                     
        }
       
        public long FibonacciNumber(long n)
        {
            long a = 0;
            long b = 1;
            long temp = 0;

            if (n == 0)
                return 0;
            else if (n < 0)
            {
                //Calculate FibonacciNumber by subtracting if the number is negative
                //if number is < -92 it will cause a 64-bit integer overflow
                if (n < -92)
                    throw new Exception("Fib(<92) will cause a 64-bit integer overflow.\r\nParameter name: n");

                for (long i = n; i < 0; i++)
                {
                    temp = a;
                    a = b;
                    b = temp - b;
                }
            }
            else
            {
                //if number is > 92 it will cause a 64-bit integer overflow
                if (n > 92)
                    throw new Exception("Fib(>92) will cause a 64-bit integer overflow.\r\nParameter name: n");

                //In N steps compute Fibonacci sequence iteratively.
                for (long i = 0; i < n; i++)
                {
                    temp = a;
                    a = b;
                    b = temp + b;
                }
            }
            
            return a;
        }
                
        public TriangleType WhatShapeIsThis(int a, int b, int c)
        {
            //if one side lese equale 0
            if (a < 1 || b < 1 || c < 1)
                return TriangleType.Error;
            else if ((long)a + (long)b <= c || (long)b + (long)c <= a || (long)c + (long)a <= b)
                return TriangleType.Error;  // Sides can't possible connect -- illegal
            else if (a == b && b == c)
                return TriangleType.Equilateral;                      
            else if (a == b || b == c || a == c)
                return TriangleType.Isosceles;
            else
                return TriangleType.Scalene;
        }


        /// <summary>
        /// Split phrase to word blocks over space char
        /// </summary>
        /// <param name="Word">Words phrase</param>
        /// <returns>Words List</returns>
        private List<string> SplitStringOverSpace(string Word)
        {
            List<string> tempArray = new List<string>();
            tempArray.Add("");

            foreach (char c in Word)
            {
                //check if current char is space , So current word is finished and recreate new word
                if (c == ' ')
                {
                    tempArray.Add(" ");
                    tempArray.Add("");
                }
                else //add current char to current word
                    tempArray[tempArray.Count - 1] += c;
            }

            return tempArray;
        }
    }
}
